package com.hdyg.sixg.activity.home;

import android.text.TextUtils;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.allen.library.SuperTextView;
import com.hdyg.sixg.R;
import com.hdyg.sixg.activity.common.BaseActivity;
import com.hdyg.sixg.entry.ManageTransBean;
import com.hdyg.sixg.mvp.Contrant.CManageTrans;
import com.hdyg.sixg.mvp.base.BaseUrl;
import com.hdyg.sixg.mvp.base.GetParamUtil;
import com.hdyg.sixg.mvp.base.SpMsg;
import com.hdyg.sixg.mvp.presenter.PManageTrans;
import com.hdyg.sixg.util.StringUtil;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;

import butterknife.BindView;

/**
 * @author CZB
 * @describe 投资理财转出
 * @time 2019/4/26 19:02
 */
public class MnageTransActivity extends BaseActivity implements CManageTrans.IVManageTrans {

    @BindView(R.id.ll_top_left)
    LinearLayout llTopLeft;
    @BindView(R.id.tvTopTitle)
    TextView tvTopTitle;
    @BindView(R.id.sv_number)
    SuperTextView svNumber;
    @BindView(R.id.sv_fee)
    SuperTextView svFee;
    @BindView(R.id.sv_fee_money)
    SuperTextView svFeeMoney;
    @BindView(R.id.et_pay_pwd)
    EditText etPayPwd;
    @BindView(R.id.tv_des)
    TextView tvDes;
    @BindView(R.id.bt_sure)
    Button btSure;
    @BindView(R.id.srl_refresh)
    SmartRefreshLayout srlRefresh;

    private CManageTrans.IPManageTrans mPresenter;
    private String id, trapass;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_manage_trans;
    }

    @Override
    protected void initView() {
        id = getIntent().getStringExtra(SpMsg.INTENT_STR);
        onClickListener();
        tvTopTitle.setText(R.string.redemption_title);
    }

    private void onClickListener() {
        srlRefresh.setOnRefreshListener(refreshLayout -> {
            srlRefresh.finishRefresh(2000);//传入false表示刷新失败
            initData();
        });
        llTopLeft.setOnClickListener(v -> finish());

        //转出
        btSure.setOnClickListener(v -> {
            trapass = etPayPwd.getText().toString().trim();
            if (TextUtils.isEmpty(id)) {
                return;
            }
            if (TextUtils.isEmpty(trapass)) {
                toastNotifyShort(R.string.pay_pwd_hint);
                return;
            }
            mPresenter.pGetApplyTrans(BaseUrl.HOST_TRANS_URL, GetParamUtil.getManageTransParam(id, trapass));
        });
    }

    @Override
    protected void initData() {
        mPresenter.pGetInit(BaseUrl.MANAGE_TRANS_INIT_URL, GetParamUtil.getManageTransInitParam(id));
    }

    @Override
    protected void createPresenter() {
        mPresenter = new PManageTrans(this);
    }

    @Override
    public void vGetInitSuccess(ManageTransBean dataBean) {
        svNumber.setRightString(dataBean.getAmount());
        String per = StringUtil.getPercentage(Float.valueOf(dataBean.getRate()), 100);
        svFee.setRightString(per);
        svFeeMoney.setRightString(dataBean.getFee());
        String des = String.format(getResources().getString(R.string.manage_trans_des), dataBean.getDay(), dataBean.getDay(), per);
        tvDes.setText(des);
    }

    @Override
    public void vGetApplyTransSuccess(String dataBean) {
        toastNotifyShort(dataBean);
        setResult(RESULT_OK);
        finish();
    }
}
